package cn.edu.baiyunu.myapplication.ui.components

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.delay

/**
 * Toast消息组件，用于显示临时提示信息
 * 
 * @param message 要显示的消息
 * @param isSuccess 是否为成功消息（影响背景颜色）
 * @param duration 显示持续时间(毫秒)
 */
@Composable
fun Toast(
    message: String,
    isSuccess: Boolean = true,
    duration: Long = 3000
) {
    var visible by remember { mutableStateOf(true) }
    val backgroundColor = if (isSuccess) {
        Color(0xFF26DE81).copy(alpha = 0.2f)
    } else {
        Color(0xFFFF5E62).copy(alpha = 0.2f)
    }
    
    LaunchedEffect(message) {
        visible = true
        delay(duration)
        visible = false
    }
    
    // 使用AnimatedVisibility在顶部渐入渐出显示Toast
    Box(
        modifier = Modifier.padding(top = 20.dp),
        contentAlignment = Alignment.TopCenter
    ) {
        AnimatedVisibility(
            visible = visible && message.isNotEmpty(),
            enter = fadeIn(),
            exit = fadeOut()
        ) {
            Box(
                modifier = Modifier
                    .clip(RoundedCornerShape(10.dp))
                    .background(backgroundColor)
                    .padding(horizontal = 20.dp, vertical = 15.dp)
            ) {
                Text(
                    text = message,
                    color = Color.White,
                    fontSize = 14.sp
                )
            }
        }
    }
} 